<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
  String path = request.getContextPath();
  String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
  String _id=request.getParameter("_");
%>
<form id="form_duty_input" class="form-horizontal">
  <input type="hidden" name="keyId" id="keyId" value="${keyId}"/>
  <input type="hidden" name="_departmentId" id="_departmentId" value="${departmentId}"/>
  <div id="container_duty_input"></div>
</form>

<script id="templates_duty_input" type="text/x-jsrender">

  <div class="form-group">
    <label class="col-md-3 control-label">部门</label>
    <div class="col-md-7">
      <input type="hidden" name="departmentId" id="departmentId" value="{{:departmentId}}"/>
      <input type="text" name="departmentName" id="departmentName" value="{{:departmentName}}" class="form-control" disabled>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">职位</label>
    <div class="col-md-7">
      <select name="powerId" id="powerId" data-live-search="true" data-size="6" class="form-control selectpicker" title="请选择职位">
      </select>
    </div>
  </div>
  <div class="form-group">
    <label class="col-md-3 control-label">成员</label>
    <div class="col-md-7">
      <input type="hidden" name="userId" id="userId" value="{{:userId}}"/>
      <input type="text" name="userName" id="userName" value="{{:userName}}" onclick="fn_member_dialog_select()" class="form-control"  placeholder="请选择成员">
    </div>
  </div>

</script>

<script id="templates_power_select" type="text/x-jsrender">

  <option data-tokens="{{:postPinYinHead}} {{:postPinYin}} {{:postName}}" value="{{:id}}">{{:postName}}</option>

</script>

<script>

  $(function(){

    // 加载数据
    fn_duty_input_load();

  });

  // 初始化表单数据
  function fn_duty_input_load() {
    var $templates = $.templates('#templates_duty_input');
    var $container = $('#container_duty_input');
    var keyId = $('#keyId').val();
    var departmentId = $('#_departmentId').val();

    var url;
    var data;
    if (keyId) {
      url = '<%=path%>/main/duty/read/' + keyId;
    } else {
      url = '<%=path%>/main/duty/init';
      if (departmentId) {
        data = {departmentId: departmentId};
      }
    }
    __ajax_get(url, data, function(data) {
      if (data.state == 1) {
        var entity = data.data;

        // 初始化模板
        var html = $templates.render(entity);
        $container.html(html);

        // 表单数据校验
        fn_duty_input_validate();

        // 岗位下拉
        $('#powerId').selectpicker({
          style: 'btn-white'
        });
        fn_duty_input_power_load(entity.departmentId, function () {
          if (entity.powerId) {
            // 默认选中
            $('#powerId').selectpicker('val', entity.powerId);
          }
        });

      }
    });
  }

  function fn_duty_input_power_load(departmentId, success) {
    var url = '<%=path%>/main/power/list_select';
    var $templates = $('#templates_power_select');
    var $container = $('#powerId');
    __ajax_get(url, {departmentId: departmentId}, function(data) {
      if (data.state == 1) {
        var list = data.data;

        // 初始化模板
        if (list) {
          var html = $templates.render(list);
          $container.html(html);
          $container.selectpicker('refresh');

          // 回调
          success();
        } else {
          __toastr_warning('部门未设置默认职权');
        }
      }
    });
  }

  function fn_member_dialog_select() {
    var userId = $('#userId').val();
    __open_dialog_select('选择成员', '<%=path%>/main/member/dialog?userId='+userId, function (dialogRef) {
      var callback = function (data) {
        if (data) {
          dialogRef.close();

          // 获取 user
          $('#userId').val(data.id);
          $('#userName').val(data.name)
        } else {
          dialogRef.enableButtons();
        }
      };
      fn_dialog_member_select(callback);
    });
  }

  // 表单数据校验
  function fn_duty_input_validate() {
    __init_validate('form_duty_input', {
      rules : {
        powerId : {
          required : true
        },
        userName : {
          required : true
        }
      },
      messages : {
        powerId : {
          required : '请选择职位'
        },
        userName : {
          required : '请选择成员'
        }
      }
    });
  }

  function fn_duty_input_save(callback) {
    if (!$('#form_duty_input').valid()) {
      return false;
    }
    __confirm_dialog(null, '确定提交么?',
            function () {
              __form_save('form_duty_input', '<%=path%>/main/duty/save', function(data) {
                if(data.state == 1){

                  callback(data.data);
                } else {
                  callback();
                }
              });
            },
            function () {
              callback();
            }
    );
  }

</script>